package com.lab.thread;   

import java.util.List;

import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;

import com.lab.driver.BaseDriver;
import com.lab.dto.DataView;
import com.lab.dto.KVMapper;
import com.lab.pojo.Monitor;
import com.lab.pojo.MonitorDAO;
import com.lab.pojo.Sensor;
import com.lab.pojo.SensorDAO;
import com.lab.pojo.Sensorproperty;
import com.lab.pojo.SensorpropertyDAO;
import com.lab.pojo.UserDAO;

/**
 * @author wuhao
 * @version 创建时间：Apr 7, 2013 9:48:50 PM
 * 类说明
 */
public class DataThread implements Runnable {
	
	WebApplicationContext context = ContextLoader.getCurrentWebApplicationContext();
	MonitorDAO monitorDao = (MonitorDAO) context.getBean("MonitorDAO");
	SensorDAO sensorDAO = (SensorDAO)context.getBean("SensorDAO");
	SensorpropertyDAO sensorpropertyDAO = (SensorpropertyDAO)context.getBean("SensorpropertyDAO");
	
	
	public void run() {
		while(true){
			try {
				Thread.sleep(500);
				List<Monitor> monitorList = monitorDao.findAll();
				for(Monitor item : monitorList){
					List<Sensor> sensorList = sensorDAO.findByProperty("monitor_id", item.getId());
					for(Sensor senItem : sensorList){
						List<Sensorproperty> sensorpropertyList = sensorpropertyDAO.findByProperty("sensor_property_name", senItem.getId());
						Class clazz = com.lab.driver.Driver_TH802.class;
						BaseDriver bDriver = (BaseDriver) clazz.newInstance();
						bDriver.doSocket(item, senItem);
						List<KVMapper> kvMapperList = bDriver.getData();
						for(Sensorproperty sensorpropertyItem : sensorpropertyList){
							for(KVMapper kvItem : kvMapperList){
								if(kvItem.getKey().equals(sensorpropertyItem.getSensorPropertyName())){
									DataView.put(sensorpropertyItem.getId(),kvItem);
								}
							}
						}
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
	}

}
  